var permuteUnique = function(nums) {

  const ans = [], path = [], used = new Array(nums.length).fill(false);
  nums.sort((a, b) => a - b);

  backtracking(nums, ans, path, used);

  return ans;
};

function backtracking(nums, ans, path, used){
  if (path.length === nums.length) {
    ans.push([...path]);
    return;
  }

  for (let i = 0; i < nums.length; i++) {
    // 同层去重
    if (i > 0 && nums[i] === nums[i - 1] && !used[i - 1]) {
      continue;
    }
    // 当前路径去重
    if(used[i]) {
      continue;
    }
    path.push(nums[i]);
    used[i] = true;
    backtracking(nums, ans, path, used);
    path.pop();
    used[i] = false;
  }

}

const nums = [1,1,2];
console.log(permuteUnique(nums));